package com.leet.code.lcof;

import java.util.Arrays;
import java.util.LinkedList;

public class Code57_2 {
    public static void main(String[] args) {

        System.out.println(Arrays.deepToString(new Code57_2().findContinuousSequence(100000)));

    }


    public int[][] findContinuousSequence(int target) {
        if (target <= 2) return new int[0][0];

        int l = (target >> 1) + (target & 1);
        int sum = 0;
        LinkedList<int[]> list = new LinkedList<>();
        LinkedList<Integer> la = new LinkedList<>();
        for (int i = 0; i <= l; ) {
            if (sum == target) {
                int[] is = new int[la.size()];
                int j = 0;
                for (Integer a : la) {
                    is[j] = a;
                    j++;
                }
                list.add(is);
                i++;
                la.add(i);
                sum = sum + i - la.removeFirst() - la.removeFirst();
            } else if (sum < target) {
                i++;
                la.add(i);
                sum = sum + i;
            } else {
                sum = sum - la.removeFirst();
            }

        }
        int[][] res = new int[list.size()][];
        int i = 0;
        for (int[] is : list) {
            res[i] = is;
            i++;
        }

        return res;
    }
}
